package com.cpi.cornpp.purchaseorder.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchaseorder.form.PurchaseOrderVersionQueryForm;
import com.cpi.cornpp.purchaseorder.service.PurchaseOrderVersionService;
import com.cpi.cornpp.transports.service.TransportService;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;

public class ShowPurchaseOrderVersionListAction extends Action {
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private static final String SUCCESS_FORWARD = "toSuccess";
	private static final String SHOW_VERSION_LIST_FORWARD = "showVersionList";
	private static final String FAILURE_FORWARD = "error";
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws PurchaseProgramException{
		int orderField = 0;
		int orderType = 0;
		String versionId = null;
		PurchaseOrderVersionQueryForm povQueryForm = null;
		PurchaseOrderVersionService povService = null;
		List purchaseOrderVersionsView = null;
		TransportService transportService = null;
		List transportListViews = null;
		HttpSession session = null;
		ActionForward forward = null;
		SystemParameterCollection systemParameterCollection = null;
		SystemParameterBean systemParameterBean = null;
		Integer transitType = null;
		Integer transportId = null;
		
		forward = mapping.findForward(FAILURE_FORWARD);
		session = request.getSession(false);
		
		try{
			
			if (form != null){
				povService = new PurchaseOrderVersionService();
				transportService = new TransportService();
				transportListViews = new ArrayList(0);
				systemParameterCollection = SystemParameterCollection.getInstance();
				systemParameterBean = systemParameterCollection.getSystemParameterBean("PERIODS_NUMBER_PO_IN_BALANCES");
				
				povQueryForm = (PurchaseOrderVersionQueryForm) form;
				
				
				versionId = (povQueryForm.getVersionId() != null && !povQueryForm.getVersionId().trim().equals("")) ? povQueryForm.getVersionId() : (String) session.getAttribute("versionId");
				
				if ((versionId != null) && StringUtils.isNumeric(versionId)){
					orderField = (StringUtils.isNotEmpty(povQueryForm.getOrderField())) ? Integer.parseInt(povQueryForm.getOrderField()) : 1;
					orderType = (StringUtils.isNotEmpty(povQueryForm.getOrderType())) ? Integer.parseInt(povQueryForm.getOrderType()) : 0;
					
					if(povQueryForm.getTransportIdFilter() > -1){
						transportId = new Integer(povQueryForm.getTransportIdFilter());
					}
					if(povQueryForm.getTransitType() > -1){
						transitType = new Integer(povQueryForm.getTransitType());
					}			
					purchaseOrderVersionsView = povService.getPurchaseOrderVersionsByVersion(new Integer(versionId), transportId, transitType, orderField, (orderType == 1));
					
					povQueryForm.setVersionId(versionId);
					
					transportListViews = transportService.getTransports();
					
					session.setAttribute("previousOrderField", Integer.toString(orderField));
					session.setAttribute("previousOrderType", Integer.toString(orderType));
					request.setAttribute("purchaseOrderVersions", purchaseOrderVersionsView);
					request.setAttribute("PERIODS_NUMBER_PO_IN_BALANCES", systemParameterBean);
					request.setAttribute("transportListViews", transportListViews);

					forward = mapping.findForward(SUCCESS_FORWARD);
				} else {
					forward = mapping.findForward(SHOW_VERSION_LIST_FORWARD);
				}
			}
			
		} catch(PurchaseProgramException ppe){
			session.setAttribute(CUSTOM_EXCEPTION, ppe);
			throw ppe;
		} catch(Exception e){
			PurchaseProgramException ppe=null;
			e.printStackTrace(System.out);
			ppe= new PurchaseProgramException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			session.setAttribute(CUSTOM_EXCEPTION, ppe);
			throw ppe;
		}
		
		return forward;
	}
}